package com.angel.service.ServiceImpl;

import com.angel.bean.RecordsDetils;
import com.angel.bean.dto.BabyRecordsDto;
import com.angel.mapper.RecordsDetilsDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.angel.bean.BabyRecords;
import com.angel.mapper.BabyRecordsDao;
import com.angel.service.BabyRecordsService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class BabyRecordsServiceImpl implements BabyRecordsService {

    @Resource
    private BabyRecordsDao babyRecordsDao;
    @Resource
    private RecordsDetilsDao recordsDetilsDao;

    @Override
    public int insert(BabyRecords babyRecords) {
        return babyRecordsDao.insert(babyRecords);
    }

    @Override
    public int insertSelective(BabyRecords babyRecords) {
        return babyRecordsDao.insertSelective(babyRecords);
    }

    @Override
    public int insertList(List<BabyRecords> babyRecordss) {
        return babyRecordsDao.insertList(babyRecordss);
    }

    @Override
    public int update(BabyRecords babyRecords) {
        return babyRecordsDao.update(babyRecords);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Map insertAndDetils(BabyRecords babyRecords, List<RecordsDetils> recordsDetils) {
        int result = babyRecordsDao.insert(babyRecords);
        Map map = new HashMap();
        map.put("result", false);
        if (result != 0) {
            for (int i = 0; i < recordsDetils.size(); i++) {
                recordsDetils.get(i).setBrid(babyRecords.getId());
                recordsDetils.get(i).setState(1);
                int tmp = recordsDetilsDao.insert(recordsDetils.get(i));
                if (tmp == 0) {
                    Exception e = new Exception("添加失败");
                    map.put("result", true);
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    try {
                        throw e;
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            }

        } else {
            Exception e = new Exception("添加失败");
            map.put("result", true);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            try {
                throw e;
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        return map;
    }

    @Override
    public List<BabyRecordsDto> findRcord(BabyRecords babyRecords) {
        return babyRecordsDao.selectAllhasName(babyRecords);
    }

    @Override
    public BabyRecords findOneRecordByBid(Integer id) {
        if (id == null) {
            return null;
        }
        BabyRecords babyRecords = new BabyRecords();
        babyRecords.setId(id);
        List<BabyRecords> recordsList = babyRecordsDao.selectAll(babyRecords);
        if (recordsList != null && !recordsList.isEmpty()) {
            return recordsList.get(0);
        }
        return null;
    }

    @Override
    public List<BabyRecords> findAll(BabyRecords babyRecords) {
        return babyRecordsDao.selectAll(babyRecords);
    }
}
